package com.ikas.ai.server.module;

import com.ikas.ai.BaseException;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.module.condition.service.WorkFlowInstanceResultService;
import com.ikas.ai.server.module.morningpaper.render.MorningPaperVariablesRender;
import com.ikas.ai.server.module.morningpaper.service.RuntimeDataStatisticsService;
import com.ikas.ai.server.module.steady.service.SteadyStateMeteService;
import com.ikas.ai.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("init")
@Slf4j
public class InitController {

    @Autowired
    private WorkFlowInstanceResultService workFlowInstanceResultService;
    @Autowired
    private RuntimeDataStatisticsService runtimeDataStatisticsService;
    @Autowired
    private MorningPaperVariablesRender morningPaperVariablesRender;

    @Resource
    private InitRedis initRedis;
    @Autowired
    SteadyStateMeteService steadyStateMeteService;

    /**
     * 运行数据上报生成
     *
     * @return
     */
    @GetMapping("runtimeDataStatistics")
    public JsonResult runtimeDataStatistics() {
        log.info("===============>init runtimeDataStatistics start<===============");
        runtimeDataStatisticsService.runtimeDataStatistics();
        log.info("===============>init runtimeDataStatistics end<===============");
        return JsonResult.ok();
    }

    @GetMapping("updateSteadyStateMete")
    public JsonResult updateSteadyStateMete() {
        log.info("===============>init updateSteadyStateMete start<===============");
        steadyStateMeteService.updateSteadyStateMete();
        log.info("===============>init updateSteadyStateMete end<===============");
        return JsonResult.ok();
    }

    @GetMapping("paper")
    public JsonResult paper() {
        log.info("===============>init paper start<===============");
        morningPaperVariablesRender.generateANewOne();
        log.info("===============>init paper end<===============");
        return JsonResult.ok();
    }

    @GetMapping("initWorkFlowInstanceResult")
    public JsonResult statistics() {
        log.info("===============>init machineStartStatistics start<===============");
        workFlowInstanceResultService.initWorkFlowInstanceResult();
        log.info("===============>init machineStartStatistics end<===============");
        return JsonResult.ok();
    }

//    @GetMapping("statisticsAppraise")
//    public JsonResult statisticsAppraise() {
//        log.info("===============>init machineStartStatistics start<===============");
//        appraiseResultService.syncAppraiseResult();
//        log.info("===============>init machineStartStatistics end<===============");
//        return JsonResult.ok();
//    }

    @GetMapping("/redis")
    public JsonResult initRedis() {
        log.info("================>start init redis<================");
        initRedis.initDataMeteCache();
        initRedis.initBaseMeteCache();
        initRedis.initTripMeteCache();
        initRedis.initWorkFlowStartMeteCache();

        log.info("================>finish init redis<================");
        return JsonResult.ok();
    }

    @GetMapping("/initDataMeteCache")
    public JsonResult initDataMeteCache() {
        log.info("手动重置t_data_mete表缓存开始");
        initRedis.initDataMeteCache();
        log.info("手动重置t_data_mete表缓存完成");
        return JsonResult.ok();
    }

    @GetMapping("/initBaseMeteCache")
    public JsonResult initBaseMeteCache() {
        log.info("手动重置t_base_mete表缓存开始");
        initRedis.initBaseMeteCache();
        log.info("手动重置t_base_mete表缓存完成");
        return JsonResult.ok();
    }

    @GetMapping("/clearByKey")
    public JsonResult clearByKey(String key) {
        if (StringUtils.isBlank(key)) {
            throw new BaseException("Invalid key");
        }
        log.info("手动清理缓存{}开始", key);
        RedisUtils.del(key);
        log.info("手动清理缓存{}完成", key);
        return JsonResult.ok();
    }
}
